
var gulp = require('gulp');
var del = require('del');

var less = require('gulp-less');
var concat = require('gulp-concat');
var cleanCss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var fileinclude = require('gulp-file-include');

var browserSync = require('browser-sync').create();

var reload      = browserSync.reload;


gulp.task('clean',function () {//清除文件
    del(['./build/**/*.*'])
});

gulp.task('lib',function () {
    gulp.src('./src/lib/**/*.*')
        .pipe(gulp.dest('./build/lib/'))
});

gulp.task('html',function () {//html合并处理
     gulp.src('./src/*.html')
         .pipe(fileinclude({prefix:'@@',basepath:'@file'}))
         .pipe(gulp.dest('./build/'))
         .pipe(reload({stream: true}));

});

gulp.task('less',function () {//less编译处理
    gulp.src('./src/less/index.less')
        .pipe(less())
        .pipe(gulp.dest('./src/css/'))
});

gulp.task('css',function () {//css压缩处理
    gulp.src('./src/css/*.css')
        .pipe(cleanCss())
        .pipe(gulp.dest('./build/css/'))
        .pipe(reload({stream: true}));
});

gulp.task('img',function () {//图片压缩处理
    gulp.src('./src/img/*.*')
        .pipe(imagemin())
        .pipe(gulp.dest('./build/img/'))
        .pipe(reload({stream: true}));
});

gulp.task('js',function () {//js压缩处理
    gulp.src('./src/js/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('./build/js/'))
        .pipe(reload({stream: true}));
});

gulp.task('serve',['clean','build'],function () {

    browserSync.init({
        server: {
            baseDir: "./build/"
        }
    });
    gulp.watch('src/lib/**/*.*',['lib']);
    gulp.watch('src/*.html',['html']);
    gulp.watch('src/less/*.less',['less']);
    gulp.watch('src/css/*.css',['css']);
    gulp.watch('src/img/*.*',['img']);
    gulp.watch('src/js/*.js',['js']);
});


gulp.task('build',['lib','img','less','css','js','html']);

gulp.task('default',['serve']);